﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PPAP.Model.Entity;
using PPAP.BO;

namespace PPAP.Web.SearchReport
{
    public partial class PPARSearch : PageBase
    {
        PPARBO bo = new PPARBO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                bindData();
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            bindData();
        }

        public void bindData()
        {
            PPAP.Model.Search.PPARSearch ps = new Model.Search.PPARSearch();
            ps.PartNo = this.tbPartno.Text;
            ps.SupplierNo = this.tbSupplierNo.Text;
            if (this.DatePicker1.Text != "")
                ps.StartDate1 = base.ToDateTime(this.DatePicker1.Text, "error format!");
            if (this.DatePicker2.Text != "")
                ps.StartDate2 = base.ToDateTime(this.DatePicker2.Text, "error format!");
            ps.SamplingReason = this.tbSamplingReason.Text;
            ps.Version = this.tbVersion.Text;
            if (this.ddlStatus.SelectedValue != "")
                ps.Status = base.ToInt32(this.ddlStatus.SelectedValue, "error format!");
            ps.MyCase = this.chkMycase.Checked;
            ps.CreateUser = CurrentUser.Id;

            DataTable dt = bo.GetPPARHistory(ps).Tables[0];
           // dt.Columns.Add("ShowStatus");
            foreach (DataRow r in dt.Rows)
            {
                r["ShowStatus"] = ShowStatus(r["status"].ToString());
            }
            AspNetPager2.RecordCount = dt.Rows.Count;
            dgList.DataSource = GetPageTable(dt, AspNetPager2.PageSize, AspNetPager2.CurrentPageIndex);
            dgList.DataBind();
        }

        protected void AspNetPager2_PageChanged(object sender, EventArgs e)
        {
            btnSubmit_Click(null, null);
        }

        public static DataTable GetPageTable(DataTable dt, int pageSize, int pageIndex)
        {
            if (dt != null)
            {
                DataTable dt1 = new DataTable();

                int pageEnd = pageIndex * pageSize;
                int pagebegin = (pageIndex - 1) * pageSize;


                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    dt1.Columns.Add(new DataColumn(dt.Columns[i].ColumnName));
                }

                for (int i = pagebegin; i < pageEnd; i++)
                {
                    if (i < dt.Rows.Count)
                    {
                        dt1.ImportRow(dt.Rows[i]);
                    }

                }
                return dt1;
            }
            else
                return null;
        }

        //状态显示
        public string ShowStatus(string Status)
        {
            string result = "";
            result = "";

            switch (Status.ToLower())
            {
                case "0":
                    result = "";
                    break;
                case "1":
                    result = "PPAR created";
                    break;
                case "2":
                    result = "PPAR submitted";
                    break;
                case "4":
                    result = "not ready for sampling";
                    break;
                case "3":
                    result = "Ready for sampling";
                    break;
                case "5":
                    result = "PPAR circulating";
                    break;
                case "6":
                    result = "PPAR released";
                    break;
                case "7":
                    result = "PPAR decided";
                    break;

                default:
                    result = "view";
                    break;
            }
            return result;
        }

        /// <summary>
        /// Export
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnExport_Click(object sender, EventArgs e)
        {
            PPAP.Model.Search.PPARSearch ps = new Model.Search.PPARSearch();
            ps.PartNo = this.tbPartno.Text;
            ps.SupplierNo = this.tbSupplierNo.Text;
            if (this.DatePicker1.Text != "")
                ps.StartDate1 = base.ToDateTime(this.DatePicker1.Text, "error format!");
            if (this.DatePicker2.Text != "")
                ps.StartDate2 = base.ToDateTime(this.DatePicker2.Text, "error format!");
            ps.SamplingReason = this.tbSamplingReason.Text;
            ps.Version = this.tbVersion.Text;
            if (this.ddlStatus.SelectedValue != "")
                ps.Status = base.ToInt32(this.ddlStatus.SelectedValue, "error format!");
            ps.MyCase = this.chkMycase.Checked;
            ps.CreateUser = CurrentUser.Id;

            DataSet ds = bo.GetPPARHistory(ps);
            //ds.Tables[0].Columns.Add("ShowStatus");
           
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                r["ShowStatus"] = ShowStatus(r["status"].ToString());
                r["d1"] = r["PPARDATE"].ToString() == "" ? r["PPARDATE"] : DateTime.Parse(r["PPARDATE"].ToString()).ToString("yyyy-MM-dd");
                r["d2"] = r["ENDTIME"].ToString() == "" ? r["ENDTIME"] : DateTime.Parse(r["ENDTIME"].ToString()).ToString("yyyy-MM-dd");
            }
            ds.Tables[0].Columns.Remove("ID");
            ds.Tables[0].Columns.Remove("BDNTREVISIONNO");
            ds.Tables[0].Columns.Remove("BDNTREVISIONSTATUS");
            ds.Tables[0].Columns.Remove("REASONFORSAMPLING");
            ds.Tables[0].Columns.Remove("STATUS");
            ds.Tables[0].Columns.Remove("DECISIONTEXT");
            ds.Tables[0].Columns.Remove("EXPLANATIONS");
            //ds.Tables[0].Columns.Remove("FINALRESULT");
            ds.Tables[0].Columns.Remove("RESUBMISSIONDATE");
            ds.Tables[0].Columns.Remove("DSC");
            ds.Tables[0].Columns.Remove("CONTACTTEL");
            ds.Tables[0].Columns.Remove("ADDRESS");
            //ds.Tables[0].Columns.Remove("SUPPLIERCODE");
            ds.Tables[0].Columns.Remove("PPARNO1");
            ds.Tables[0].Columns.Remove("BDNTPARTNAME");
            ds.Tables[0].Columns.Remove("SUPPLIERNAME");
            ds.Tables[0].Columns.Remove("SUPPLIERPARTNO");
            ds.Tables[0].Columns.Remove("PPARDATE");
            ds.Tables[0].Columns.Remove("ENDTIME");
            ds.Tables[0].Columns["ShowStatus"].ColumnName="Status";

            ds.Tables[0].Columns["ROWNUM"].ColumnName = "No.";
            ds.Tables[0].Columns["BDNTPARTNO"].ColumnName = "Part.No";
            ds.Tables[0].Columns["PPARNO"].ColumnName = "PPAR.No";
            ds.Tables[0].Columns["QLEVEL"].ColumnName = "Q-level";
            ds.Tables[0].Columns["SUPPLIERCODE"].ColumnName = "Supplier Code";
            ds.Tables[0].Columns["PPARVERSION"].ColumnName = "PPAR Version";
            ds.Tables[0].Columns["d1"].ColumnName = "Start date";
            ds.Tables[0].Columns["d2"].ColumnName = "End date";
            ds.Tables[0].Columns["FINALRESULT"].ColumnName = "Result";
            ds.Tables[0].Columns["REMARKS"].ColumnName = "Remark";
            CreateExcel(ds, "PPAR");
        }
    }
}